import { eachNext, _type } from '../../utils/index.js'
import { _mid, _dis, _polar, _atan, _k, _sin, _centre } from '../index.js'
import { _mirror, } from '../mirror.js'

// 计算控制点
export const calc = (p1, p2, {
    radiusRatio = 1,
    angleOffset = 0,
    orient  //向心
} = {}) => {
    let mid = _mid(p1, p2)
    let r = _dis(p1, mid)
    let cp1 = _polar(mid, radiusRatio * r, orient ? _atan(p1, p2) - 90 + angleOffset : angleOffset)
    let cp2 = _mirror(cp1, mid)
    return {
        p1,
        p2,
        cp1,
        cp2,
        mid,
        r
    }
}


// 曲线
export const curvePoints = (points, props) => {
    let ps = []
    let cs = []
    eachNext(points, (t, index, next) => {
        let p = calc(t, next, props)
        cs[cs.length] = p.cp1
        cs[cs.length] = p.cp2

    })
    eachNext(points, (t, index, next) => {
        ps[ps.length] = t
        ps[ps.length] = [...cs[2 * index], ...next]
    })
    return ps
}

// 波形
export const wavePoints = (points, props) => {
    let ps = []
    let cps = []
    let mids = []
    eachNext(points, (t, index, next) => {
        let p = calc(t, next, props)
        cps[cps.length] = p.cp1
        cps[cps.length] = p.cp2
        mids[mids.length] = p.mid

    })
    eachNext(points, (t, index, next) => {
        ps[ps.length] = t
        ps[ps.length] = [...cps[2 * index], ...mids[index]]
        ps[ps.length] = [...cps[2 * index + 1], ...next]
    })
    return ps
}


// 锯齿形
export const sawtoothPoints = (points, props) => {
    let ps = []
    let cps = []
    eachNext(points, (t, index, next) => {
        let p = calc(t, next, props)
        cps[cps.length] = p.cp1
        cps[cps.length] = p.cp2

    })
    eachNext(points, (t, index, next) => {
        ps[ps.length] = t
        ps[ps.length] = cps[2 * index]
        ps[ps.length] = cps[2 * index + 1]
        ps[ps.length] = next
    })
    return ps
}


// 半圆弧
export const semicirclePoints = (points, {
    sweepFlag,
    radiusRatio = 1,
    xAxisRotation = 0,
    largeArcFlag,
    closed,
    broken,
    recycleIndex
} = {}) => {
    // A rx ry x-axis-rotation large-arc-flag sweep-flag x y
    let ps = []
    // let n = _recycle(points, recycleIndex)
    eachNext(points, (t, index, next) => {
        let r = _dis(t, next) / 2
        // ps[ps.length] = t
        // ps[ps.length] = [this.r, this.r, xAxisRotation, largeArcFlag ? 1 : 0, sweepFlag ? 1 : 0, ...next]
        ps[ps.length] = [t, [r, r, xAxisRotation, largeArcFlag ? 1 : 0, sweepFlag ? 1 : 0, ...next]]
    })
    // return _d(ps, closed, broken)
    return ps
}

// 椭圆弧 elliptical arc 椭圆弧
export const ellipticalPoints = (points, {
    sweepFlag,
    radiusRatio = 1,
    xAxisRotation = 0,
    largeArcFlag,
    recycleIndex
} = {}) => {
    // A rx ry x-axis-rotation large-arc-flag sweep-flag x y
    let ps = []
    // let n = _recycle(points, recycleIndex)
    eachNext(points, (t, index, next) => {
        // let p = calc(t, next)
        // let r = p.r

        let r = _dis(t, next) / 2
        // let r = this.rs[index]
        ps[ps.length] = t
        ps[ps.length] = [r, radiusRatio * r, xAxisRotation, largeArcFlag ? 1 : 0, sweepFlag ? 1 : 0, ...next]
    })
    return ps
}



export const sinPoints = (points, options) => {
    let {
        k = 0, a = 0, w = 1
    } = options //  num = 36, r = 100,
    let ps = []
    let o = _centre(points),
        r = _dis(o, points[0]),
        num = _k(r / 3, 0)
    eachNext(points, (t, index, next) => {
        let m = _mid(t, next)
        a = _atan(o, m)
        ps.push(...Array.from({
            length: num,
        },
            (t, i) => [
                _k((i * 360) / num + o[0] - 180),
                _k(r * _sin(w * ((i * 360) / num - 180) - a) + o[1] - k),
            ]
        ))
    })
    return ps
}



